package com.example.startapp.fragment;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.startapp.R;
import com.example.startapp.activity.LoginActivity;
import com.example.startapp.entity.User;
import com.example.startapp.util.DBOpenHelper;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.zip.Inflater;

import butterknife.BindInt;
import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * 会员中心
 */

public class MineFragment extends Fragment implements View.OnClickListener {
    private View view;
    private Inflater inflater;
    private DBOpenHelper mDBOpenHelper;
    int[] images = {
            R.mipmap.tx1,
            R.mipmap.tx2,
            R.mipmap.tx3,
            R.mipmap.tx4,
            R.mipmap.tx5


    };


    //   @BindView(R.id.not)
//    LinearLayout lin_not;
    @BindView(R.id.image_tx)
    ImageView image_tx;
    @BindView(R.id.btn_login)
    TextView btn_login;//登录注册窗口里的 按钮
    @BindView(R.id.login_success)
    RelativeLayout re_loginsuccess;//登录后的个人信息
    @BindView(R.id.name)
    TextView loginname;
    @BindView(R.id.text_id)
    TextView loginid;
    @BindView(R.id.intent_person) RelativeLayout intent_person;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.mine, null);
        ButterKnife.bind(this, view);
        mDBOpenHelper = new DBOpenHelper(getActivity());
        init();


        return view;
    }


    private void init() {
        btn_login.setOnClickListener(this);
        intent_person.setOnClickListener(this);
        LinearLayout lin_not = view.findViewById(R.id.not);

        //如果有数据
        if (mDBOpenHelper.getAllData().size() > 0) {
            lin_not.setVisibility(View.GONE);
            re_loginsuccess.setVisibility(View.VISIBLE);
//            ArrayList<User> list = new ArrayList<>();
            SQLiteDatabase db = mDBOpenHelper.getReadableDatabase();
            Cursor cursor = db.query("user", null, null, null, null, null, "id ASC");
            cursor.moveToLast();
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String getId = mDBOpenHelper.registerData(name);

            image_tx.setImageResource(images[Integer.parseInt(getId)]);
            loginname.setText(name);
            loginid.setText("NO.00" + getId);


        } else {
            lin_not.setVisibility(View.VISIBLE);//未登录/注册 显示 登录/注册 按钮
            re_loginsuccess.setVisibility(View.GONE);
        }


    }


    @Override
    public void onClick(View v) {
        Intent intent_loginto = new Intent(getActivity(), LoginActivity.class);
        startActivity(intent_loginto);

    }
}
